import Vue from 'vue';

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-default/index.css';
Vue.use(ElementUI);

import VueRouter from 'vue-router';
Vue.use(VueRouter);

import Vuex from 'vuex';
Vue.use(Vuex);

import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

import App from './App';
import store from './store/';
import routes from './config/router.js';

import 'mixin/';
import 'plugins/';

const router = new VueRouter({ routes });

router.beforeEach((to, from, next) => {
    window.scroll(0, 0);

    if (!store.state.user.userinfo.token && to.path !== '/login') {
        store.dispatch('remove_userinfo');
        next('/login');
    } else {
        if (store.state.user.userinfo.token && to.path === '/login') {
            next({
                path: '/business/users/list'
            });
        } else {
            NProgress.start();
            next();
        }
    }
})

router.afterEach(transition => {
    NProgress.done();
});

const appAdmin = new Vue({
    el: '#app',
    data() {
        return {};
    },
    store,
    router,
    render: h => h(App)
})